package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JDBCDemo12 {
    public static void main(String[] args) {
        Connection con=null;
        try {
            con=DBUtil.getConnection();
            //通过Connection可以设置事务的隔离级别
            con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
            con.setAutoCommit(false);
            String sql="INSERT INTO userinfo(username,password,nickname,age) "+
                       "VALUES(?,?,?,?)";
            PreparedStatement ps=con.prepareStatement(sql);
            ps.setString(1,"jack");
            ps.setString(2,"123456");
            ps.setString(3,"mr.j");
            ps.setInt(4,25);
            int num=ps.executeUpdate();
            System.out.println(num>0?"插入成功":"插入失败");
            con.commit();

        } catch (SQLException e) {
            try {
                if (con!=null) {
                    con.rollback();
                }
            } catch (SQLException ex) {
                e.printStackTrace();
            }
        }finally {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
